package com.glorysoft.lipuxinpda.activity

import android.annotation.SuppressLint
import android.os.SystemClock
import android.view.View
import com.glorysoft.lipuxinpda.R
import com.glorysoft.lipuxinpda.base.Constant.DEFAULT_TIME_OUT
import com.glorysoft.lipuxinpda.base.Constant.FILE_URL
import com.glorysoft.lipuxinpda.base.Constant.NET_URL
import com.glorysoft.lipuxinpda.utils.MyUtils.isFastClick
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils
import kotlinx.android.synthetic.main.activity_setting.*

/**
 * FileName: SettingActivity
 * Author: XJH
 * Date: 2021/4/8 17:48
 * Description:
 * History:
 */
class SettingActivity : FreeUI_BaseActivity<Any?>(), View.OnClickListener{
    private val COUNTS = 5 // 点击次数
    private var mHits = LongArray(COUNTS) //记录点击次数
    private val DURATION: Long = 2000 //有效时间
    private var freeuiGeneralfragmentdialog: FreeUI_GeneralFragmentDialog? = null
    override fun getLayoutResId(): Int {
        return R.layout.activity_setting
    }

    override fun initView() {
        freeUI_titleView.setTitle("设置")
    }

    @SuppressLint("SetTextI18n")
    override fun initData() {
        tvVersion.text = FreeApi_Utils.getVerName()
        tvIP.text = NET_URL
        tvFileIP.text = FILE_URL
        tvTimeOut.text = DEFAULT_TIME_OUT + "s"
        rlIP.setOnClickListener(this)
        rlFileIP.setOnClickListener(this)
    }

    override fun getBaseEntity(): FreeUI_AddViewEntity {
        return FreeUI_AddViewEntity(
                intent.getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY),
                llMain
        )
    }

    override fun onClick(view: View) {
        if (isFastClick) {
            showToast("您点击的太快了", false)
            return
        }
        when (view.id) {
            R.id.rlIP -> {
                freeuiGeneralfragmentdialog =
                        FreeApi_DialogUtils.getScanDialog(this, "请输入IP地址", NET_URL) { s ->
                            freeuiGeneralfragmentdialog!!.dismiss()
                            NET_URL = s
                            tvIP!!.text = NET_URL
                            FreeApi_SharePreferencesUtils.setSharePre(
                                    "URL_FILE",
                                    "URL_IP",
                                    s,
                                    applicationContext
                            )
                            showToast("保存成功", true)
                        }
                freeuiGeneralfragmentdialog?.show(supportFragmentManager, "URL_IP_DIALOG")
            }
            R.id.rlFileIP -> {
                freeuiGeneralfragmentdialog =
                        FreeApi_DialogUtils.getScanDialog(this, "请输入IP地址", FILE_URL) { s ->
                            freeuiGeneralfragmentdialog!!.dismiss()
                            FILE_URL = s
                            tvFileIP!!.text = FILE_URL
                            FreeApi_SharePreferencesUtils.setSharePre(
                                    "URL_FILE",
                                    "FILE_IP",
                                    s,
                                    applicationContext
                            )
                            showToast("保存成功", true)
                        }
                freeuiGeneralfragmentdialog?.show(supportFragmentManager, "FILE_IP_DIALOG")
            }
            R.id.rlTimeOut -> {
                freeuiGeneralfragmentdialog = FreeApi_DialogUtils.getScanDialog(
                        this,
                        "请输入超时时间",
                        DEFAULT_TIME_OUT
                ) { s ->
                    freeuiGeneralfragmentdialog!!.dismiss()
                    DEFAULT_TIME_OUT = s
                    tvTimeOut!!.text = DEFAULT_TIME_OUT + "s"
                    FreeApi_SharePreferencesUtils.setSharePre(
                            "TIME_OUT_FILE",
                            "TIME_OUT2",
                            s,
                            applicationContext
                    )
                    showToast("保存成功，请重启APP生效", true)
                    rlTimeOut!!.visibility = View.GONE
                }
                freeuiGeneralfragmentdialog?.show(supportFragmentManager, "TIME_OUT_DIALOG")
            }
            R.id.llCaiDan -> {
                //将mHints数组内的所有元素左移一个位置
                System.arraycopy(mHits, 1, mHits, 0, mHits.size - 1)
                //获得当前系统已经启动的时间
                mHits[mHits.size - 1] = SystemClock.uptimeMillis()
                if (mHits[0] >= SystemClock.uptimeMillis() - DURATION) {
                    mHits = LongArray(COUNTS)
                    rlTimeOut!!.visibility = View.VISIBLE
                }
            }
        }
    }
}